#include <iostream>
#include "BlockQueue.hpp"
#include "Task.hpp"
#include <unistd.h>
#include <ctime>


void *Consumer(void *args)
{
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);

    while (1)
    {
        // sleep(2);
        // 获取
        // int t;
        // Task ta;
        task_t t;
        bq->Pop(&t);
        // 执行任务
        t();
        // std::cout << "consumer-> " << ta.PrintRet() << std::endl;
    }
}

void *Productor(void *args)
{
    srand(time(nullptr));
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);

    while(1)
    {
        // 生产数据
        // int x = rand() % 10 + 1; // [1, 10]
        // int y = rand() % 10 + 1; // [1, 10]

        // Task ta(x, y);
        bq->Equeue(Download);

        // std::cout << "productor-> " << ta.PrintTask() << std::endl;
        std::cout << "productor-> Download" << std::endl;

        sleep(1);
    }
}

int main()
{
    // 阻塞队列
    BlockQueue<task_t> *bq = new BlockQueue<task_t>();

    // 生产者线程和消费者线程
    pthread_t c, p;
    pthread_create(&c, nullptr, Consumer, bq);
    pthread_create(&p, nullptr, Productor, bq);

    // join
    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    return 0;
}